package cn.ekuma.android.keyBoardinput;

import com.android.calculator2.ColorButton;
import com.android.calculator2.EventListener;
import com.android.calculator2.Logic;
import com.android.calculator2.CalculatorDisplay;
import com.android.calculator2.PanelSwitcher;
import com.android.calculator2.Logic.OnReturnListener;

import cn.ekuma.android.shard.R;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;

public class KeyBoradInputPanel extends LinearLayout implements PanelSwitcher.Listener, Logic.Listener{

	 EventListener mListener;
	 private Logic mLogic;
	 private View mClearButton;
	 private View mBackspaceButton;
	 private View mOverflowMenuButton;
	 private CalculatorDisplay mDisplay;
	 private View mainView;
     private ColorButton panelCtrol;
     private LinearLayout viewPager;
        
	public KeyBoradInputPanel(Context context) {
		super(context);
		init(context);
    }

    public KeyBoradInputPanel(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	private void init(Context context) {

		 mListener = new EventListener();
		
		 mainView = LayoutInflater.from(context).inflate( 
              R.layout.keyboard_input, this); 

       if (mClearButton == null) {
           mClearButton = mainView.findViewById(R.id.clear);
           mClearButton.setOnClickListener(mListener);
           mClearButton.setOnLongClickListener(mListener);
       }
       if (mBackspaceButton == null) {
           mBackspaceButton = mainView.findViewById(R.id.del);
           mBackspaceButton.setOnClickListener(mListener);
           mBackspaceButton.setOnLongClickListener(mListener);
       }


       mDisplay =(CalculatorDisplay)mainView.findViewById(R.id.display);

       mLogic = new Logic(context, mDisplay);
       mLogic.setListener(this);

       mLogic.setLineLength(mDisplay.getMaxDigits());


       mListener.setHandler(mLogic, null);
       mDisplay.setOnKeyListener(mListener);
       mDisplay.addTextChangedListener(new TextWatcher(){

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
			fireValueChanged(s.toString());	
		}

		@Override
		public void afterTextChanged(Editable s) {
			// TODO Auto-generated method stub
			
		} 
    	   
       });
       
       
       viewPager=(LinearLayout) mainView.findViewById(R.id.simplePad);
       
       panelCtrol=(ColorButton) mainView.findViewById(R.id.showhide_panel);
       panelCtrol.setOnClickListener(new OnClickListener(){

		public void onClick(View v) {
			boolean isExpand=viewPager.isShown();
			viewPager.setVisibility(isExpand?View.GONE:View.VISIBLE);
			mDisplay.requestLayout();
			fireStateChanged(!isExpand);
		}   
       });
       

       //if (!ViewConfiguration.get(this).hasPermanentMenuKey()) {
       //    createFakeMenu();
       //}
       initCheckListener();

       updateDeleteMode();
		
	}

	 public void setInputListener(OnReturnListener listener) {
		 mLogic.setInputListener(listener);
	 }


	private void updateDeleteMode() {
		
        if (mLogic.getDeleteMode() == Logic.DELETE_MODE_BACKSPACE) {
            mClearButton.setVisibility(View.GONE);
            mBackspaceButton.setVisibility(View.VISIBLE);
        } else {
            mClearButton.setVisibility(View.VISIBLE);
            mBackspaceButton.setVisibility(View.GONE);
        }
    }

	 
    public interface ViewStateChangeListener {
        void onExpand(boolean isExpand);
        
        
    }
    
    ViewStateChangeListener stateChangeListener;
	
	public ViewStateChangeListener getStateChangeListener() {
		return stateChangeListener;
	}

	public void setStateChangeListener(ViewStateChangeListener stateChangeListener) {
		this.stateChangeListener = stateChangeListener;
	}
    
    public void fireStateChanged(boolean isExpand){
    	if(stateChangeListener!=null)
    		stateChangeListener.onExpand(isExpand);
    }
	
	
    
   public void setOnClickListener(int id) {
        final View target =mainView.findViewById(id);
        target.setOnClickListener(mListener);
    }
   
   
	private void initCheckListener(){
		 final Resources res = getResources();
         final TypedArray simpleButtons = res.obtainTypedArray(R.array.simple_buttons);
         for (int i = 0; i < simpleButtons.length(); i++) {
             setOnClickListener(simpleButtons.getResourceId(i, 0));
         }
         simpleButtons.recycle();

         //final TypedArray advancedButtons = res.obtainTypedArray(R.array.advanced_buttons);
        //for (int i = 0; i < advancedButtons.length(); i++) {
         //    setOnClickListener(advancedPage, advancedButtons.getResourceId(i, 0));
        // }
        // advancedButtons.recycle();

         final View clearButton = findViewById(R.id.clear);
         if (clearButton != null) {
             mClearButton = clearButton;
         }

         final View backspaceButton = findViewById(R.id.del);
         if (backspaceButton != null) {
             mBackspaceButton = backspaceButton;
         }
	}

    public void onDeleteModeChange() {
        updateDeleteMode();
    }
	
    public void reset(double i) {
    	mLogic.insert(String.valueOf(i));
	}
    
    
    public interface ValueChangeListener {
        void onValueChanged(String newValue);
        
    }
    
    ValueChangeListener valueChangeListener;
    
    public ValueChangeListener valueChangeListener() {
		return valueChangeListener;
	}

	public void setValueChangeListener(ValueChangeListener stateChangeListener) {
		this.valueChangeListener = stateChangeListener;
	}
    
    public void fireValueChanged(String value){
    	if(valueChangeListener!=null)
    		valueChangeListener.onValueChanged(value);
    }
    
    public Double getDoubleValue(){
    	String text=mDisplay.getText().toString();
        if (text == null || text.equals("")) {
            return null; 
        } else {
            try {
                return Double.parseDouble(text);
            } catch (NumberFormatException e) {
                return null;
            }
        }
    } 
    
    public Integer getValueInteger() {  
    	String text=mDisplay.getText().toString();
        if (text == null || text.equals("")) {
            return null; 
        } else {
           try {
              return Integer.parseInt(text);
           } catch (NumberFormatException e) {
        	  return null;
           }
        }
    }

	@Override
	public void onChange() {
		// TODO Auto-generated method stub
	}
	
	public void initData(){
		mLogic.setListener(null);
		mLogic.clear(true);
		updateDeleteMode();
		mLogic.setListener(this);
	}

}
